using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class PhieuNhapKhoDAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
		public ResultObj SavePhieuNhapKho(PhieuNhapKho phieunhapkho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spAddPhieuNhapKho", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spAddPhieuNhapKho";
			SqlParameter param = new SqlParameter();
			param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, phieunhapkho.Id);
			cmd.Parameters.Add(param);
			param = CreateParameter("@IdBangKe", DbType.Int32, ParameterDirection.Input, phieunhapkho.IdBangKe);
			cmd.Parameters.Add(param);
			param = CreateParameter("@MaSo", DbType.String, ParameterDirection.Input, phieunhapkho.MaSo);
			cmd.Parameters.Add(param);
			param = CreateParameter("@NguoiLap", DbType.String, ParameterDirection.Input, phieunhapkho.NguoiLap);
			cmd.Parameters.Add(param);
			param = CreateParameter("@NgayThang", DbType.DateTime, ParameterDirection.Input, phieunhapkho.NgayThang);
			cmd.Parameters.Add(param);
			param = CreateParameter("@KeToanTruong", DbType.String, ParameterDirection.Input, phieunhapkho.KeToanTruong);
			cmd.Parameters.Add(param);
			param = CreateParameter("@NguoiGiaoHang", DbType.String, ParameterDirection.Input, phieunhapkho.NguoiGiaoHang);
			cmd.Parameters.Add(param);
			param = CreateParameter("@NoiDung", DbType.String, ParameterDirection.Input, phieunhapkho.NoiDung);
			cmd.Parameters.Add(param);
			param = CreateParameter("@SoHoaDonDo", DbType.String, ParameterDirection.Input, phieunhapkho.SoHoaDonDo);
			cmd.Parameters.Add(param);
			param = CreateParameter("@Thue", DbType.Int64, ParameterDirection.Input, phieunhapkho.Thue);
			cmd.Parameters.Add(param);
			param = CreateParameter("@IdChiNhanhCTy", DbType.Int32, ParameterDirection.Input, phieunhapkho.IdChiNhanhCTy);
			cmd.Parameters.Add(param);
            param = CreateParameter("@IsDelete", DbType.Int32, ParameterDirection.Input, phieunhapkho.IsDelete);
            cmd.Parameters.Add(param);
            param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
            cmd.Parameters.Add(param);
			conObj.Open();
			cmd.ExecuteNonQuery();
			conObj.Close();
            result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			return result;
		}
		
		// Get all PhieuNhapKho in DB with IsDelete = 0
		public ResultObj GetAllPhieuNhapKho( )
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<PhieuNhapKho> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllPhieuNhapKho", CommandType.StoredProcedure);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<PhieuNhapKho>();
			PhieuNhapKho item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new PhieuNhapKho();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdBangKe = Convert.ToInt32(reader["IdBangKe"]);
			item.MaSo = Convert.ToString(reader["MaSo"]);
			item.NguoiLap = Convert.ToString(reader["NguoiLap"]);
			item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
			item.KeToanTruong = Convert.ToString(reader["KeToanTruong"]);
			item.NguoiGiaoHang = Convert.ToString(reader["NguoiGiaoHang"]);
			item.NoiDung = Convert.ToString(reader["NoiDung"]);
			item.SoHoaDonDo = Convert.ToString(reader["SoHoaDonDo"]);
			item.Thue = Convert.ToInt64(reader["Thue"]);
			item.IdChiNhanhCTy = Convert.ToInt32(reader["IdChiNhanhCTy"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}

        // Get all PhieuNhapKho in DB
        public ResultObj GetAllPhieuNhapKhoToCheckCode()
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            List<PhieuNhapKho> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetAllPhieuNhapKhoToCheckCode", CommandType.StoredProcedure);
            cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<PhieuNhapKho>();
                PhieuNhapKho item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new PhieuNhapKho();
                    item.Id = Convert.ToInt32(reader["Id"]);
                    item.IdBangKe = Convert.ToInt32(reader["IdBangKe"]);
                    item.MaSo = Convert.ToString(reader["MaSo"]);
                    item.NguoiLap = Convert.ToString(reader["NguoiLap"]);
                    item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
                    item.KeToanTruong = Convert.ToString(reader["KeToanTruong"]);
                    item.NguoiGiaoHang = Convert.ToString(reader["NguoiGiaoHang"]);
                    item.NoiDung = Convert.ToString(reader["NoiDung"]);
                    item.SoHoaDonDo = Convert.ToString(reader["SoHoaDonDo"]);
                    item.Thue = Convert.ToInt64(reader["Thue"]);
                    item.IdChiNhanhCTy = Convert.ToInt32(reader["IdChiNhanhCTy"]);
                    item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
                    lst.Add(item);
                }
            }
            result.Result = lst;
            result.ReturnCode = 1;

            return result;
        }
		
		// Get all PhieuNhapKho in DB with condition
		public ResultObj GetAllPhieuNhapKhoByID(PhieuNhapKho phieunhapkho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<PhieuNhapKho> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllPhieuNhapKhoByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, phieunhapkho.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<PhieuNhapKho>();
			PhieuNhapKho item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new PhieuNhapKho();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdBangKe = Convert.ToInt32(reader["IdBangKe"]);
			item.MaSo = Convert.ToString(reader["MaSo"]);
			item.NguoiLap = Convert.ToString(reader["NguoiLap"]);
			item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
			item.KeToanTruong = Convert.ToString(reader["KeToanTruong"]);
			item.NguoiGiaoHang = Convert.ToString(reader["NguoiGiaoHang"]);
			item.NoiDung = Convert.ToString(reader["NoiDung"]);
			item.SoHoaDonDo = Convert.ToString(reader["SoHoaDonDo"]);
			item.Thue = Convert.ToInt64(reader["Thue"]);
			item.IdChiNhanhCTy = Convert.ToInt32(reader["IdChiNhanhCTy"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}

        // Get all PhieuNhapKho in DB with condition
        public ResultObj GetAllPhieuNhapKhoByMaSo(PhieuNhapKho phieunhapkho)
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            List<PhieuNhapKho> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetAllPhieuNhapKhoByMaSo", CommandType.StoredProcedure);
            SqlParameter param = CreateParameter("@MaSo", DbType.String, ParameterDirection.Input, phieunhapkho.MaSo);
            cmd.Parameters.Add(param);
            cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<PhieuNhapKho>();
                PhieuNhapKho item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new PhieuNhapKho();
                    item.Id = Convert.ToInt32(reader["Id"]);
                    item.IdBangKe = Convert.ToInt32(reader["IdBangKe"]);
                    item.MaSo = Convert.ToString(reader["MaSo"]);
                    item.NguoiLap = Convert.ToString(reader["NguoiLap"]);
                    item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
                    item.KeToanTruong = Convert.ToString(reader["KeToanTruong"]);
                    item.NguoiGiaoHang = Convert.ToString(reader["NguoiGiaoHang"]);
                    item.NoiDung = Convert.ToString(reader["NoiDung"]);
                    item.SoHoaDonDo = Convert.ToString(reader["SoHoaDonDo"]);
                    item.Thue = Convert.ToInt64(reader["Thue"]);
                    item.IdChiNhanhCTy = Convert.ToInt32(reader["IdChiNhanhCTy"]);
                    item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
                    lst.Add(item);
                }
            }
            result.Result = lst;
            result.ReturnCode = 1;

            return result;
        }
		
		// Delete a PhieuNhapKho out of DB with condition
		public ResultObj DeletePhieuNhapKho(PhieuNhapKho phieunhapkho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spDeletePhieuNhapKho", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spDeletePhieuNhapKho";
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, phieunhapkho.Id);
			cmd.Parameters.Add(param);
			conObj.Open();
			result.ReturnCode = cmd.ExecuteNonQuery();
			conObj.Close();
			
			return result;
		}
		
		// Get info of CTPhieuNhapKho from DB with condition
		public ResultObj GetCTPhieuNhapKho(int IdCTPhieuNhapKho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<CTPhieuNhapKho> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetCTPhieuNhapKhoByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdCTPhieuNhapKho);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<CTPhieuNhapKho>();
			CTPhieuNhapKho item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new CTPhieuNhapKho();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdPhieuNhapKho = Convert.ToInt32(reader["IdPhieuNhapKho"]);
			item.MaVatLieu = Convert.ToString(reader["MaVatLieu"]);
			item.TenVatLieu = Convert.ToString(reader["TenVatLieu"]);
			item.DonViTinh = Convert.ToString(reader["DonViTinh"]);
			item.DonGia = Convert.ToInt64(reader["DonGia"]);
			item.ThanhTien = Convert.ToInt64(reader["ThanhTien"]);
			item.Tong = Convert.ToInt64(reader["Tong"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get info of ChiNhanhCongTy from DB with condition
		public ResultObj GetChiNhanhCongTy(int IdChiNhanhCongTy)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<ChiNhanhCongTy> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetChiNhanhCongTy", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdChiNhanhCongTy);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<ChiNhanhCongTy>();
			ChiNhanhCongTy item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new ChiNhanhCongTy();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.TenChiNhanh = Convert.ToString(reader["TenChiNhanh"]);
			item.DiaChiChiNhanh = Convert.ToString(reader["DiaChiChiNhanh"]);
			item.SoDienThoai = Convert.ToString(reader["SoDienThoai"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
					
	}
}
